package com.ruoyi.teaching.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.Gson;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.teaching.domain.WebSocketMessage;
import com.ruoyi.teaching.domain.vo.ChatMessageVO;
import com.ruoyi.teaching.mapper.WebSocketMessageMapper;
import com.ruoyi.teaching.service.WebSocketMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 李志鑫
 * @since 2024-08-23
 */
@Service
public class WebSocketMessageServiceImpl extends ServiceImpl<WebSocketMessageMapper, WebSocketMessage> implements WebSocketMessageService {

    @Autowired
    private WebSocketMessageMapper webSocketMessageMapper;
    @Autowired
    private RedisCache redisCache;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public AjaxResult selectChatMessage(Integer pageNum, Integer pageSize, Long dialogueId, String message) {
        int personId = SecurityUtils.getUserId().intValue();
        System.out.println("-------------------------------------------");
        QueryWrapper<WebSocketMessage> webSocketMessageQueryWrapper = new QueryWrapper<>();
        if (dialogueId==0) {
            webSocketMessageQueryWrapper
                    .and(w->w.isNull("user_id").eq("dialogue_id", dialogueId))
                    .or(w->w.eq("dialogue_id", dialogueId).eq("user_id",personId));
        }else {
            webSocketMessageQueryWrapper.eq("dialogue_id", dialogueId);
        }
        if (null != message && !message.equals("")) {
            webSocketMessageQueryWrapper.like("message", message);
        }
        List<WebSocketMessage> webSocketMessages = webSocketMessageMapper.selectList(webSocketMessageQueryWrapper);
        System.out.println("----------------------------");
        return new AjaxResult(HttpStatus.SUCCESS, "成功", webSocketMessages);
    }

    private Lock lock = new ReentrantLock();

    @Override
    public AjaxResult insertChatMessage(ChatMessageVO chatMessageVO) {
        lock.lock();
        try {
            if (chatMessageVO.getName().equals("0")&&chatMessageVO.getDialogueId().toString().equals("0")) {
                    WebSocketMessage webSocketMessage = new WebSocketMessage();
                    webSocketMessage.setMessage(chatMessageVO.getText());
                    webSocketMessage.setDialogueId(0);
                    webSocketMessage.setUserId(chatMessageVO.getUserId());
                    webSocketMessageMapper.insert(webSocketMessage);
//                    添加系统通知关联

            }else{
                Gson gson = new Gson();
                String message = gson.toJson(chatMessageVO);
                WebSocketMessage webSocketMessage = new WebSocketMessage();
                webSocketMessage.setDialogueId(chatMessageVO.getDialogueId());
                webSocketMessage.setMessage(message);
                Integer insert = webSocketMessageMapper.insert(webSocketMessage);
                redisCache.deleteCacheMapValue("chatMessage", chatMessageVO.getDialogueId().toString());
                if (insert == 0) {
                    return new AjaxResult(HttpStatus.SUCCESS, "聊天记录存储失败");
                }
            }
        } catch (Exception e) {
            //打印异常
            e.printStackTrace();
        } finally {
            //释放锁
            lock.unlock();
        }
        return new AjaxResult(HttpStatus.SUCCESS, "成功");

    }

    @Override
    public AjaxResult deleteChatMessage(Long dialogueId) {
        QueryWrapper<WebSocketMessage> webSocketMessageQueryWrapper = new QueryWrapper<>();
        webSocketMessageQueryWrapper.eq("dialogue_id", dialogueId);
        webSocketMessageQueryWrapper.isNull("user_id");
        int delete = webSocketMessageMapper.delete(webSocketMessageQueryWrapper);
        if (delete == 0) {
            return new AjaxResult(HttpStatus.ERROR, "删除失败");
        }
        return new AjaxResult(HttpStatus.SUCCESS, "成功");
    }
}
